package cn.jsj.invite.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.jsj.invite.dao.InvitationDao;
import cn.jsj.invite.dao.SchoolDao;
import cn.jsj.invite.dao.StudentDao;
import cn.jsj.invite.dao.SuperAdminDao;
import cn.jsj.invite.pojo.*;
import cn.jsj.invite.service.SuperAdminService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SuperAdminServiceImpl implements SuperAdminService {


    private final SuperAdminDao superAdminDao;
    private final StudentDao studentDao;
    private final SchoolDao schoolDao;

    private final InvitationDao invitationDao;

    public SuperAdminServiceImpl(SuperAdminDao superAdminDao, StudentDao studentDao, SchoolDao schoolDao, InvitationDao invitationDao) {
        this.superAdminDao = superAdminDao;
        this.studentDao = studentDao;
        this.schoolDao = schoolDao;
        this.invitationDao = invitationDao;
    }

    @Override
    public SuperAdminInfo login(String loginName, String password) {
        SuperAdminInfo superAdminInfo = superAdminDao.selectOneSuperAdminByNameAndPassword(loginName, password);
        superAdminInfo.setSuperName("");
        superAdminInfo.setSuperPassword("");
        return superAdminInfo;
    }

    @Override
    public PageInfo<StudentEnt> pageStudentsInfo(Integer schoolId, Integer page) {
        page = page != null ? page : 1;
        //开始分页，一页15条
        PageHelper.startPage(page, 15);
        List<StudentEnt> students = studentDao.selectPageStudentBySchoolId(schoolId);
        //导航页显示的页码为5页，进行分页
        return new PageInfo<>(students, 5);
    }

    @Override
    public Integer addOneSchoolAdmin(String name, String password, Integer schoolId, String phone) {
        return schoolDao.insertOneSchoolAdmin(name, password, schoolId, phone);
    }

    @Override
    public Integer updatePasswordById(String oldPassword, String newPassword, Integer id) {
        if (oldPassword != null && newPassword != null && id > 0) {
            return superAdminDao.updateSuperPasswordByIdInt(oldPassword, newPassword, id);
        }
        return 0;
    }

    @Override
    public Integer deleteSchoolAdminByAdminId(Integer schoolAdminId, Integer schoolId) {
        if (ObjectUtil.hasNull(schoolAdminId,schoolId)){
            return null;
        }
        SchoolInfo schoolInfo = schoolDao.selectOneSchoolById(schoolId);
        List<SchoolAdminEnt> schoolAdminEnts = schoolDao.selectOneSchoolAdminById(schoolAdminId.longValue());
        if (ObjectUtil.hasNull(schoolAdminEnts,schoolInfo)){
            return null;
        }
        return superAdminDao.deleteSchoolAdminByAdminId(schoolAdminId, schoolId);
    }

    @Override
    public PageInfo<InvitationInfo> listAllPageInfoWithSchoolId(Integer schoolId, Integer page) {
        page = page != null ? page : 1;
        //开始分页，一页15条
        PageHelper.startPage(page, 10);
        List<InvitationInfo> invitationInfos = superAdminDao.selectAllPageWithSchoolId(schoolId);
        InvitationServiceImpl.invitationInfoDataFormat(invitationInfos);
        //导航页显示的页码为5页，进行分页
        return new PageInfo<>(invitationInfos, 5);
    }

    @Override
    public List<InvitationInfo> listAllPageInfo(Integer superAdminId) {
        SuperAdminInfo superAdminInfo = superAdminDao.selectOneSuperAdminById(superAdminId);
        if (superAdminInfo == null) {
            return null;
        }
        List<InvitationInfo> infos = invitationDao.selectAllInvitation();
        InvitationServiceImpl.invitationInfoDataFormat(infos);
        return infos;
    }

    @Override
    public List<StudentEnt> listAllStudent(Integer superId) {
        SuperAdminInfo superAdminInfo = superAdminDao.selectOneSuperAdminById(superId);
        if (superAdminInfo == null) {
            return null;
        }
        return studentDao.selectAllStudent();
    }

    @Override
    public List<SchoolAdminEnt> listAllSchoolAdmin(Integer superAdminId) {
        SuperAdminInfo superAdminInfo = superAdminDao.selectOneSuperAdminById(superAdminId);
        if (superAdminInfo == null) {
            return null;
        }
        return superAdminDao.selectAllSchoolAdmin();
    }

    @Override
    public Integer updateSchoolAdminPassword(Integer schoolAdminId, String newPassword) {
        if (schoolAdminId == null){
            return null;
        }
        if (schoolDao.selectSchoolAdminById(schoolAdminId.longValue()) == null){
            return null;
        }
        return schoolDao.updatePasswordById(schoolAdminId,newPassword);

    }
}
